
package com.funo.government.info.view;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.funo.government.info.R;
import com.funo.government.info.bean.NoticeItem;
import com.funo.government.info.utils.BusinessException;
import com.funo.government.info.utils.BusinessResolver.BusinessCallback;
import com.funo.government.info.utils.GovernmentResolver;
import com.funo.government.info.utils.PreferenceUtils;
import com.funo.government.info.utils.RequestTask;

public class NoticeListActivity extends BaseActivity {

    private DropDownListView lvNotice;

    private LayoutInflater mInflater;

    private RequestTask mAreaListTask;

    private int page = 1;

    private static final int SIZE = 10;

    private ArrayList<NoticeItem> mNotices;

    private NoticeListAdapter mAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_list);
        lvNotice = (DropDownListView) findViewById(R.id.lvNotice);
        mNotices = new ArrayList<NoticeItem>();
        mInflater = getLayoutInflater();
        mAdapter = new NoticeListAdapter();
        lvNotice.setAdapter(mAdapter);
        lvNotice.setOnBottomListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                requestData(++page);
            }
        });

        lvNotice.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
                NoticeItem item = mAdapter.getItem(position);
                Intent intent = new Intent(NoticeListActivity.this, NoticeDetailActivity.class);

                intent.putExtra("title", item.title);
                intent.putExtra("content", item.content);

                startActivity(intent);

            }
        });
        requestData(page);

    }

    private void requestData(int page) {
        String areaId = PreferenceUtils.getLocalAreaId(this);
        lvNotice.onBottomBegin();
        mAreaListTask = GovernmentResolver.noticeList(areaId, page, SIZE,
                new BusinessCallback<ArrayList<NoticeItem>>() {

                    @Override
                    public void onSuccess(ArrayList<NoticeItem> notices) {

                        if (notices != null && notices.size() > 0) {
                            mNotices.addAll(notices);
                            mAdapter.setData(mNotices);
                            lvNotice.setHasMore(true);
                            lvNotice.onBottomComplete();
                        }

                        else {
                            lvNotice.setHasMore(false);
                            lvNotice.onBottomComplete();
                        }
                    }

                    @Override
                    public void onError(BusinessException e) {
                        e.printStackTrace();
                        Toast.makeText(NoticeListActivity.this, R.string.cannot_connect_server,
                                Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeTask(mAreaListTask);
    }

    private void closeTask(RequestTask task) {
        if (task != null) {
            task.cancel(true);
            task = null;
        }
    }

    private class NoticeListAdapter extends BaseAdapter {

        public NoticeListAdapter() {
        }

        private ArrayList<NoticeItem> notices;

        public void setData(ArrayList<NoticeItem> noticeItems) {
            this.notices = noticeItems;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            if (notices == null)
                return 0;
            return notices.size();
        }

        @Override
        public NoticeItem getItem(int position) {
            return notices.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_notice_list, null);
                vh = new ViewHolder();
                vh.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
                vh.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            NoticeItem noticeItem = getItem(position);
            vh.tvTitle.setText(noticeItem.title);
            vh.tvContent.setText(noticeItem.content);
            return convertView;
        }

    }

    private class ViewHolder {
        TextView tvTitle;

        TextView tvContent;

    }
}
